package com.kedacom.freedomLabel.model.db.dao;

import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.OnConflictStrategy;
import android.arch.persistence.room.Query;
import android.arch.persistence.room.RoomWarnings;
import android.arch.persistence.room.Update;

import com.kedacom.freedomLabel.model.bean.Item;
import com.kedacom.freedomLabel.model.bean.ItemGroup;

import java.util.List;

/**
 * @author licheng
 * @since 2019/11/15 14:08
 */
@Dao
public interface ItemDao extends IDao<Item> {

    @Override
    @Query("SELECT COUNT(0) FROM ITEM")
    int count();

    @Override
    @Update
    void update(Item category);

    @Override
    @Insert(onConflict = OnConflictStrategy.FAIL)
    long save(Item item);

    @Override
    @Insert(onConflict = OnConflictStrategy.FAIL)
    long[] save(List<Item> items);

    @Override
    @Query("SELECT * FROM ITEM ORDER BY createTime DESC")
    List<Item> get();

    @Override
    @Query("SELECT * FROM Item WHERE category =:category ORDER BY createTime DESC")
    List<Item> get(String category);

    @Override
    @Query("SELECT * FROM ITEM WHERE category =:category AND (shortHand LIKE :keyword OR number LIKE :keyword) ORDER BY createTime DESC")
    List<Item> get(String category, String keyword);

    @Override
    @Delete
    void delete(Item item);

    @Override
    @Query("DELETE FROM ITEM")
    void delete();

    @Override
    @Query("DELETE FROM Item WHERE category =:param")
    void delete(String param);

    @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH)
    @Query("SELECT COUNT(category) AS " + ItemGroup.COLUMN_COUNT + ",category AS " + ItemGroup.COLUMN_CATEGORY + " FROM Item GROUP BY category ORDER BY category ASC")
    List<ItemGroup> group();

}